package element.base;

import ast.AstItem;

import java.util.ArrayList;
import java.util.List;

public class Synt extends BaseNode {
    private ArrayList<BaseNode> nodes;

    public Synt() {
        this.nodes = new ArrayList<>();
    }

    public void addNode(BaseNode node) {
        this.nodes.add(node);
    }

    public ArrayList<BaseNode> getNodes() {
        return nodes;
    }

    public boolean isType(Class type) {
        return this.getClass().equals(type);
    }

    public Word findFirst() {
        BaseNode node = nodes.get(0);
        if (node instanceof Word) {
            return (Word) node;
        } else {
            return ((Synt) node).findFirst();
        }
    }

    public AstItem toAst() {
        return null;
    }

    public List<AstItem> toAsts() {
        List<AstItem> list = new ArrayList<>();
        list.add(toAst());
        return list;
    }

    @Override
    public String toString() {
        String fstr = "";
        for (BaseNode node: nodes) {
            fstr += node.toString() + "\n";
        }

        fstr += "<" + this.getClass().getSimpleName() + ">";

        if (fstr.charAt(fstr.length() - 1) == '\n') {
            fstr = fstr.substring(0, fstr.length() - 1);
        }

        return fstr;
    }

    @Override
    public String toTreeString(int level) {
        String fstr = "";
        for (int i = 0; i < level; i++) {
            fstr += "\t";
        }

        fstr += "<" + this.getClass().getSimpleName() + ">\n";

        for (BaseNode node: nodes) {
            fstr += node.toTreeString(level + 1) + "\n";
        }

        if (fstr.charAt(fstr.length() - 1) == '\n') {
            fstr = fstr.substring(0, fstr.length() - 1);
        }

        return fstr;
    }
}
